const validate = require("./validate.js");

//空值过滤器
const filterForm = form => {
	let obj = {};
	Object.keys(form).forEach(ele => {
		if (!validate.validatenull(form[ele])) {
			obj[ele] = form[ele];
		}
	});
	return obj;
};

//判断是否是小程序
const isMiniPg = () => {
	let isMiniPg = false
	//#ifdef MP-WEIXIN
	isMiniPg = true
	//#endif
	//#ifdef MP-ALIPAY
	isMiniPg = true
	//#endif
	return isMiniPg
}

// 如果有分享人则给data带上分享人的user_code
const dataAddSharerUserCode = data => {
	let sharer_user_code = uni.getStorageSync('sharer_user_code')
	if (sharer_user_code) {
		data = Object.assign({
			sharerUserId: sharer_user_code
		}, data)
	}
	return data
}

// 时间格式转换
export const getDay = date => {
	const dt = date ? new Date(date) : new Date()
	const y = dt.getFullYear()
	const m = (dt.getMonth() + 1 + "").padStart(2, "0")
	const d = (dt.getDate() + "").padStart(2, "0")
	return `${y}-${m}-${d}`
}

// 近7日
export const getLSevenDay = () => {	
	var currentDate = new Date(); // 获取当前日期和时间
	currentDate.setDate(currentDate.getDate() - 7); // 将日期减去7天
	 
	var year = currentDate.getFullYear(); // 获取年份
	var month = (currentDate.getMonth() + 1).toString().padStart(2, '0'); // 获取月份，月份从0开始，所以要加1，同时使用padStart方法保证月份为两位数
	var day = currentDate.getDate().toString().padStart(2, '0'); // 获取日期，并使用padStart方法保证日期为两位数
	 
	var result = year + '-' + month + '-' + day; // 拼接年月日
	return result
}

// 提示弹窗
export const uShowModal = (content) => {
	uni.showModal({
		title: '提示', //提示的标题
		content: content, //提示的内容
		showCancel: false, // 是否显示取消按钮，默认为 true
		success: (res) => {}
	});
}

module.exports = {
	filterForm,
	isMiniPg,
	dataAddSharerUserCode,
	getDay,
	getLSevenDay,
	uShowModal
};